package com.qidian.by.cms.dto;

import com.qidian.by.pojo.ContentCategory;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: CoolPro
 * @Description:
 * @Time: 2024-05-08 10:22
 **/

public class CategoryDTO {
    private ContentCategory contentCategory;
    private List<CategoryDTO> childCategories;

    public CategoryDTO(ContentCategory contentCategory) {
        this.contentCategory = contentCategory;
        this.childCategories = new ArrayList<>();
    }

    public void addChildCategory(CategoryDTO child) {
        this.childCategories.add(child);
    }

    public void assembleChildren(List<ContentCategory> allCategories) {
        List<CategoryDTO> children = allCategories.stream()
                .filter(cc -> cc.getParentId().equals(this.contentCategory.getCatId()))
                .map(CategoryDTO::new)
                .collect(Collectors.toList());
        for (CategoryDTO child : children) {
            child.assembleChildren(allCategories); // 递归调用
            this.addChildCategory(child);
        }
    }

    public ContentCategory getContentCategory() {
        return contentCategory;
    }

    public void setContentCategory(ContentCategory contentCategory) {
        this.contentCategory = contentCategory;
    }

    public List<CategoryDTO> getChildCategories() {
        return childCategories;
    }

    public void setChildCategories(List<CategoryDTO> childCategories) {
        this.childCategories = childCategories;
    }
}
